$(function() {
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    let months = [];
    let current_month = '';
    let start_month = '';
    let months_count = '';
    let coupon_uuid = GetQueryString('coupon_uuid');
    let token = GetQueryString('token');
    let platform = GetQueryString('platform');
    let version = GetQueryString('version');
    let is_mini = GetQueryString('is_mini');
    // let version_num = Number(version.replace(/\./g,''));
    $.ajax({
        type: "GET",
        timeout: 15000,
        // url: "http://192.168.1.67:3000/api/app/v1/coupon/mommy_school_coupons/convert_choose_month?token=" + token + "&coupon_uuid=" + coupon_uuid + '&version=' + version + '&platform=' + platform,
        url: "https://kksappapi.kekeshi.com/api/app/v1/coupon/mommy_school_coupons/convert_choose_month?token=" + token + "&coupon_uuid=" + coupon_uuid + '&version=' + version + '&platform=' + platform,
        dataType: "json",
        data: {},
        success: function(response) {
            if (response.code === 200) {
            var data = response.data;
            start_month = data.start_month;
            var str = '';
            if (data.alert !== "") {
                $('.alert_text').html(data.alert);
                $('.alert_bg').css('display','block');
            }
            if (data.start_month > 36) {
                start_month = '';
            }
            months_count = data.months_count;
            current_month = data.start_month;
            $('.current_month').eq(0).html(data.month_age);
            $('.months_count').eq(0).html(data.months_count);
            str += '<div class="one_year">';
            str += '<div class="top_title_price">';
            str += '<div class="course_title">' + data.combo.title + '</div>';
            str += '<div class="price_box">';
            str += '<div class="price">&nbsp;&yen;' + data.combo.price + '&nbsp;</div>';
            str += '</div>';
            str += '</div>';
            str += '<div class="center_content_box">';
            $.each(data.combo.courses, function(n, course_obj) {
                str += '<div class="content_box">';
                str += '<div class="content_title">' + course_obj.name + '</div>';
                str += '<div class="content_number">' + course_obj.count + '节</div>';
                str += '</div>';
            });
            str += '<div style="clear:both;"></div>';
            str += '</div>';
            str += '<div class="course_desc">共计' + data.combo.course_count + '节课，每节低至' + data.combo.avg_price + '元</div>';
            str += '</div>';
            str += '</div>';
            str += '</div>';
            // if (obj.choose === true) {
                //     eq = i;
                // }
            $(".course").html(str);
            // 月份
            months = [];
            $.each(data.month_table, function(i, obj) {
                if (obj.state === 1) {
                    $('.month').eq(i).css('background','#ffc2c2');
                    $('.month').eq(i).css('color','#ffffff');
                } else if (obj.state === 2) {
                    $('.month').eq(i).css('background','#ff7d7d');
                    $('.month').eq(i).css('color','#ffffff');
                    months.push(i + 1);
                }
            });
            console.log(months)
//todo 大于三十六或者全部购卖推荐为空时 只显示套餐名字与价格
            if ((data.start_month > 36) || (data.start_month == null)) {
                $('.center_content_box').css('display','none')
                $('.course_desc').css('display','none')
            }
        } else if (response.code === 401 || response.error === "COUPON_NOT_FOUND") {
                $('.error_bg').css('display','block');
                $('.error_text').html(response.msg);
            }
            else if (response.error === "COUPON_UNAVAILABLE") {
                $(".used_bg").css('display', 'block');
                $(".used_text").html(response.msg);
            }
        },
        complete: function (XMLHttpRequest,status) {
            console.log(status)
            if (status == 'timeout') {
                $('.alert_text').html('请求已超时');
                $('.alert_bg').css('display','block');
            } else if (status == 'error') {
                $('.alert_text').html('请求失败');
                $('.alert_bg').css('display','block');
            }
        }
    });
    $('.month').on('click',function(){
        current_month = $(this).text();
        console.log(current_month);
        $.ajax({
            type: "GET",
            timeout: 15000,
            // url: "http://192.168.1.67:3000/api/app/v1/coupon/mommy_school_coupons/convert_choose_month?token=" + token + "&coupon_uuid=" + coupon_uuid + '&version=' + version + '&platform=' + platform + '&month=' + current_month,
            url: "https://kksappapi.kekeshi.com/api/app/v1/coupon/mommy_school_coupons/convert_choose_month?token=" + token + "&coupon_uuid=" + coupon_uuid + '&version=' + version + '&platform=' + platform + '&month=' + current_month,
            dataType: "json",
            data: {},
            success: function(response) {
                console.log(response.data);
                if (response.code === 200) {
                    var data = response.data;
                    var str = '';
                    if (data.alert !== "") {
                        $('.alert_text').html(data.alert);
                        $('.alert_bg').css('display','block');
                    }
                    months_count = data.months_count;
                    start_month = data.start_month;
                    current_month = data.start_month;
                    $('.current_month').eq(0).html(data.month_age);
                    $('.months_count').eq(0).html(data.months_count);
                    str += '<div class="one_year">';
                    str += '<div class="top_title_price">';
                    str += '<div class="course_title">' + data.combo.title + '</div>';
                    str += '<div class="price_box">';
                    str += '<div class="price">&nbsp;&yen;' + data.combo.price + '&nbsp;</div>';
                    str += '</div>';
                    str += '</div>';
                    str += '<div class="center_content_box">';
                    $.each(data.combo.courses, function(n, course_obj) {
                        str += '<div class="content_box">';
                        str += '<div class="content_title">' + course_obj.name + '</div>';
                        str += '<div class="content_number">' + course_obj.count + '节</div>';
                        str += '</div>';
                    });
                    str += '<div style="clear:both;"></div>';
                    str += '</div>';
                    str += '<div class="course_desc">共计' + data.combo.course_count + '节课，每节低至' + data.combo.avg_price + '元</div>';
                    str += '</div>';
                    str += '</div>';
                    str += '</div>';
                    $(".course").html(str);
                    // 月份
                    months = [];
                    $('.month').css('background','#ffffff');
                    $('.month').css('color','#333333');
                    $('.month').css('border','none');
                    $.each(data.month_table, function(i, obj) {
                        if (obj.state === 1) {
                            $('.month').eq(i).css('background','#ffc2c2');
                            $('.month').eq(i).css('color','#ffffff');
                        } else if (obj.state === 2) {
                            $('.month').eq(i).css('background','#ff7d7d');
                            $('.month').eq(i).css('color','#ffffff');
                            months.push(i + 1);
                        }
                    });
                    console.log(months)
                    if ((data.start_month > 36) || (data.start_month == null)) {
                        $('.center_content_box').css('display','none');
                        $('.course_desc').css('display','none');
                    }
                } else if (response.code === 401 || response.error === "COUPON_NOT_FOUND") {
                    $('.error_bg').css('display','block');
                    $('.error_text').html(response.msg);
                } else if (response.error === "COUPON_UNAVAILABLE") {
                    $(".used_bg").css('display', 'block');
                    $(".used_text").html(response.msg);
                }
            },
            complete: function (XMLHttpRequest,status) {
                console.log(status);
            if (status == 'timeout') {
                $('.alert_text').html('请求已超时');
                $('.alert_bg').css('display','block');
            } else if (status == 'error') {
                $('.alert_text').html('请求失败');
                $('.alert_bg').css('display','block');
            }
        }
        });
    });
    close_alert = () => {
      $('.alert_bg').css('display','none');
      $('.alert_text').html('');
    }
    explain = () => {
        if ((start_month == "") || (start_month == null)) {
            $('.tips').css('display', 'block');
            setTimeout(function(){
                $('.tips').css('display', 'none');
            },5000);
        } else {
            $('.confirm_bg').eq(0).css('display', 'block');
        }
    };

    cancel = () => {
        $('.confirm_bg').eq(0).css('display', 'none');
    };
    confirm = () => {
        $.ajax({
            type: "POST",
            timeout: 15000,
            url: "https://kksappapi.kekeshi.com/api/app/v1/coupon/mommy_school_coupons/convert_combo",
            // url: "http://192.168.1.67:3000/api/app/v1/coupon/mommy_school_coupons/convert_combo",
            dataType: "json",
            data: {
                token: token,
                coupon_uuid: coupon_uuid,
                months: months.toString(),
                platform: platform,
                version: version,
            },
            success: function(response) {
                console.log(response)
                if (response.code === 200) {
					// console.log(is_mini)
                    if (is_mini == 'true'){
                        $('.confirm_bg').eq(0).css('display', 'none');
						wx.miniProgram.navigateTo({url: '/pages/coupon/coupon'})
                    } else {
                        $('.confirm_bg').eq(0).css('display', 'none');
                        window.location.href = 'kekeshi://app.kekeshi.com/mommySchoolMarketing'
                    }
        

                } else if (response.code === 401 || response.error === "COUPON_NOT_FOUND") {
                    $('.error_bg').css('display','block');
                    $('.error_text').html(response.msg);
                } else if (response.error === "COUPON_UNAVAILABLE") {
                    $(".used_bg").css('display', 'block');
                    $(".used_text").html(response.msg);
                }
            },
            complete: function (XMLHttpRequest,status) {
                console.log(status)
                if (status == 'timeout') {
                    $('.alert_text').html('请求已超时');
                    $('.alert_bg').css('display','block');
                } else if (status == 'error') {
                    $('.alert_text').html('请求失败');
                    $('.alert_bg').css('display','block');
                }
            }
        });
    };
    $('.error_btn').click(function () {
        if (is_mini == 'true') {
			wx.miniProgram.navigateTo({url: '/pages/coupon/coupon'})
        } else {
            window.location.href = 'kekeshi://app.kekeshi.com/mommySchoolMarketing'
        }
    })
    $('.used_btn').click(() => {
        if (is_mini == 'true') {
			wx.miniProgram.navigateTo({url: '/pages/coupon/coupon'})
        } else {
            window.location.href = 'kekeshi://app.kekeshi.com/exchange'
        }
    })
});
